//
//  DetailOfBookViewController.m
//  喜马拉雅FM
//
//  Created by dllo on 16/3/2.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "DetailOfBookViewController.h"

#import "CellForType.h"

#import "ModelForTypeOfDetail.h"
#import "ModelForTypeOfListOfDetail.h"


@interface DetailOfBookViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *arrForTitle;
@property (nonatomic, retain) NSMutableArray *arrForList;


@end

@implementation DetailOfBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 系统返回键的颜色.
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];   
    [self handleData];
    [self createTableView];
}

- (void)handleData {

    self.arrForTitle = [NSMutableArray array];
    self.arrForList = [NSMutableArray array];
    
    for (ModelForTypeOfDetail *modelForTitle in self.arrForTypeOfDetail) {
   
        // 取出标题头.
        NSString *str = modelForTitle.title;
        [self.arrForTitle addObject:str];
       
        // 取出存放详情model的数组.
        self.arrForList = modelForTitle.arrForListDetail;
    }
}


- (void)createTableView {
 
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellForType class] forCellReuseIdentifier:@"poolForCellForType"];
}

// 区数.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.arrForTypeOfDetail.count;
}

// 行数.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}
// cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    
    ModelForTypeOfDetail *model = [self.arrForTypeOfDetail objectAtIndex:indexPath.section];
    CellForType *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCellForType"];

    [cell passModelForTypeOfDetail:model];
    
    return cell;
}

// 索引.
//- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView  {
//
//    return self.arrForTitle;
//}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
  
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"biaoti"]];
    image.frame = CGRectMake(10, 7, 16, 16);
    [view addSubview:image];
    [image release];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, WIDTH - 20, 30)];
    [view addSubview:label];
    label.text = self.arrForTitle[section];
    [label release];
    return view;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 30;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 300;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
